package com.top.horizon.th1804app.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.top.horizon.th1804app.R;
import com.top.horizon.th1804app.constant.Constant;
import com.top.horizon.th1804app.utils.LogUtil;

import java.util.ArrayList;
import java.util.Collections;

public class MoreFramesAdapter extends RecyclerView.Adapter<MoreFramesAdapter.MoreFramesViewHolder> implements ItemTouchHelperAdapter {
    private Context mContext;
    private MoreFramesViewHolder moreFramesViewHolder;
    private View inflate;
    private ArrayList<Integer> mData = new ArrayList<>();

    public MoreFramesAdapter(Context context, int[] p1) {
        this.mContext = context;
        for (int i = 0; i < 4; i++) {
//            Log.e("adapter",i+"==="+Constant.channelNames[p1[i]]);
            mData.add(p1[i]);
        }
    }

    @NonNull
    @Override
    public MoreFramesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        inflate = View.inflate(mContext, R.layout.item_channel, null);
        moreFramesViewHolder = new MoreFramesViewHolder(inflate);
        return moreFramesViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MoreFramesViewHolder holder, int position) {
        moreFramesViewHolder.tvChannelNumber.setText(Constant.channelNames[mData.get(position)]);
        Log.e("adapter",position+"==="+Constant.channelNames[mData.get(position)]);
    }

    @Override
    public int getItemCount() {
        return 4;
    }

    @Override
    public void onItemMove(RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
        int fromPosition = source.getAdapterPosition();
        int toPosition = target.getAdapterPosition();
        LogUtil.e("ajhdsajhd", "开始移动索引==" + fromPosition + "===结束移动索引==" + toPosition);
        if (fromPosition < mData.size() && toPosition < mData.size()) {
            //交换数据位置
            Collections.swap(mData, fromPosition, toPosition);

            //刷新位置交换
            if (Math.abs(fromPosition - toPosition) == 2) {
                if (fromPosition < toPosition) {
                    notifyItemMoved(fromPosition, fromPosition + 1);
                    notifyItemMoved(toPosition, fromPosition + 1);
                    notifyItemMoved(fromPosition, fromPosition + 1);
                } else {
                    notifyItemMoved(fromPosition, fromPosition - 1);
                    notifyItemMoved(toPosition, fromPosition - 1);
                    notifyItemMoved(fromPosition, fromPosition - 1);
                }
            } else if (Math.abs(fromPosition - toPosition) == 3) {
                notifyItemMoved(0, 1);
                notifyItemMoved(1, 2);
                notifyItemMoved(2, 3);
                notifyItemMoved(2, 1);
                notifyItemMoved(1, 0);
            } else if (Math.abs(fromPosition - toPosition) == 1) {
                notifyItemMoved(fromPosition, toPosition);
            }
            mOnTouchFramesListener.onDragView(fromPosition, toPosition);
//          notifyItemRangeChanged();
        }

    }

    @Override
    public void onItemClear(RecyclerView.ViewHolder source) {

    }


    public class MoreFramesViewHolder extends RecyclerView.ViewHolder {
        public TextView tvChannelNumber;

        public MoreFramesViewHolder(final View itemView) {
            super(itemView);
            tvChannelNumber = itemView.findViewById(R.id.tv_channel_1);


        }
    }

    private OnTouchFramesListener mOnTouchFramesListener;

    public void setOnTouchFramesListener(OnTouchFramesListener onTouchFramesListener) {
        this.mOnTouchFramesListener = onTouchFramesListener;
    }

    public interface OnTouchFramesListener {
        void onDragView(int a,int b);

        void onClickItem(ArrayList<Integer> mData);
    }
}
